package com.yuyou.tas.lesson.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yuyou.tas.lesson.service.IBaseLevelService;
import com.yuyou.tas.lesson.service.comm.OrganLimitService;
import com.yuyou.tas.service.api.lesson.dto.DtoBaseLevel;
import com.yuyou.tas.util.bean.LoginRedisObj;
import com.yuyou.tas.util.bean.PageParams;
import com.yuyou.tas.util.bean.Pagination;
import com.yuyou.tas.util.bean.ResBoolSimpleInfo;
import com.yuyou.tas.util.bean.ReturnResult;
import com.yuyou.tas.util.config.SystemConfig;

@Api(value="BaseLevelController",description="层级操作接口")
@Validated
@RestController
@RequestMapping(value="/api/BaseLevel")
public class BaseLevelController extends BaseController {

    @Autowired
    private IBaseLevelService service;

    @Autowired
    private OrganLimitService organLimitService;


    @ApiOperation(value="添加层级")
    @RequestMapping(value="/addLevel",method = RequestMethod.POST)
    public ReturnResult addLevel(@Validated @ApiParam(value = "层级名称")@NotBlank(message = "层级名称不能为空")@RequestParam(value = "name") String name,
                                  HttpServletRequest request){
        LoginRedisObj user = this.getUserObj(request);
        ResBoolSimpleInfo info = service.addLevel(name,user.getIsSystem()? 1:0,
                                                null,user,user.getOrganizationId());
        return ReturnResult.returnRes(info.isSuccess() , info.getMessage() , null);
    }

    @ApiOperation(value="删除层级")
    @RequestMapping(value="/delLevel",method = RequestMethod.POST)
    public ReturnResult delLevel(@Validated @ApiParam(value = "ID")@NotNull(message = "ID不能为空")@RequestParam(value = "id") Long id,
                                 HttpServletRequest request) throws Exception {
        LoginRedisObj user = this.getUserObj(request);
        /*if(!user.getIsSystem()) {
            organLimitService.checkOrganByOrganId(user.getOrganizationId(), id, OrganLimitEnums.type.base_level);
        }*/
        ResBoolSimpleInfo info = service.delLevel(id , user);
        return ReturnResult.returnRes(info.isSuccess() , info.getMessage() , null);
    }

    /*@ApiOperation(value="通过ID获取层级")
    @RequestMapping(value="/getBaseLevel",method = {RequestMethod.GET, RequestMethod.POST})
    public ReturnResult<BaseLevel> getBaseLevel(@Validated @NotNull(message = "ID不能为空")@ApiParam(value = "ID" , required = true) @RequestParam(value = "id") Long id,
                                                HttpServletRequest request) throws Exception {
        LoginRedisObj user = this.getUserObj(request);
        if(!user.getIsSystem()) {
            organLimitService.checkOrganByOrganId(user.getOrganizationId(), id, OrganLimitEnums.type.base_level);
        }
        ResBoolSimpleInfo info =  service.getLevelByID(id);
        return ReturnResult.returnRes(info.isSuccess() , info.getMessage() , info.getData());
    }*/

    @ApiOperation(value="修改层级")
    @RequestMapping(value="/updateLevel",method = RequestMethod.POST)
    public ReturnResult updateLevel( @Validated @NotNull(message = "ID不能为空") @ApiParam(value = "ID")@RequestParam(value = "id") Long id,
                                     @Validated @NotBlank @ApiParam(value = "层级名称")@RequestParam(value = "name") String name,
                                     HttpServletRequest request) throws Exception {
        LoginRedisObj user = this.getUserObj(request);
        /*if(!user.getIsSystem()) {
            organLimitService.checkOrganByOrganId(user.getOrganizationId(), id, OrganLimitEnums.type.base_level);
        }*/
        ResBoolSimpleInfo info = service.updateLevel(id , name , null,user.getIsSystem()?1:0 , user);
        return ReturnResult.returnRes(info.isSuccess() , info.getMessage() , null);
    }

    @ApiOperation(value = "查询层级")
    @RequestMapping(value = "/findBaseLevelPage" ,method = {RequestMethod.GET,RequestMethod.POST})
    public ReturnResult<DtoBaseLevel> findBaseLevelPage(HttpServletRequest request,
                                                        @Validated @ModelAttribute PageParams pageParams,
                                                        @ApiParam(value = "数据类型：0标准，1机构")@RequestParam(value = "type",required = false) Integer type,
                                                        @ApiParam(value = "名称")@RequestParam(value = "name",required = false) String name,
                                                        @ApiParam(value = "获取系统数据：1、是，0或不传、否")@RequestParam(value = "getSys" , required = false)Integer getSys){
        Map<String, Object> conditions = new HashMap<>();
        Pagination<DtoBaseLevel> queryPgn = new Pagination<>();
        LoginRedisObj user = this.getUserObj(request);
        if(!StringUtils.isEmpty(name)){
            conditions.put("name", name.trim());
        }

        if(type != null && type == 0){
            conditions.put("isStan", "1");//获取标准数据
        }

        if(getSys != null && getSys == 1){
            conditions.put("organ_id", SystemConfig.SYS_ORGAN_ID);//系统机构
        }else {
            conditions.put("organ_id", user.getOrganizationId());//查询个人所在机构的校区
        }

        queryPgn.setConditions(conditions);
        queryPgn.setPageParams(pageParams);

        int total = service.findLevelCount(queryPgn);
        queryPgn.setTotal(total);
        queryPgn.toBePage();
        if(total > 0){
            List<DtoBaseLevel> list = service.findLevelPageList(queryPgn);
            queryPgn.setList(list);
        }
        return ReturnResult.success(queryPgn);
    }
		
}
